package com.brayan.webapp.controller.general.i18n;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import com.brayan.webapp.service.general.i18n.TranslationBean;


@Named
@Path("/i18n/translation/")
@Consumes("*/*")
@Produces(MediaType.APPLICATION_JSON)
public class TranslationResource {

	private static Logger LOGGER = Logger.getLogger(TranslationResource.class);
	
	@Inject
	TranslationBean translationService;	
	
	@PUT
	@Path("{textId}/{localeCode}")
	public String setTranslation(String body,
			@PathParam("textId") String textId,
			@PathParam("localeCode") String localeCode, 
			@Context HttpServletRequest request){		
		
		Long textUnitId = Long.parseLong(textId);		
		translationService.saveTranslation(textUnitId, localeCode, body);	
		// Return a xml element. Otherwise PUT-Request Response is empty and throws JavaScript error.
		LOGGER.info("Saving translated text ("+body+") for text id " + textId + " and locale code " + localeCode + ".");
		return "<empty/>";
	}	
}